#####################################
##REPLICATION FOR MCGHEE, ET AL.,####
##"A PRIMARY CAUSE OF PARTISANSHIP"##
#####################################

##Figure 2##

##Load Data#
setwd("XXXX") #ENTER DIRECTORY PATHNAME IN PLACE OF XXXX

legyrd <- read.csv("legislator primaries.dem.csv", header=TRUE)
legyrr <- read.csv("legislator primaries.rep.csv", header=TRUE)

#generate election year counter#
legyrd$yrcnt <- (legyrd$elec-1992)/2
legyrr$yrcnt <- (legyrr$elec-1992)/2

close.screen(all.screens=TRUE)
split.screen(figs=c(2,3))

#CLOSED
screen(1)
Mean.Ideology <- NULL
for (yr in 0:9) {
Mean.Ideology <- c(Mean.Ideology, 
	mean(legyrd$pred_np[legyrd$yrcnt==yr & legyrd$closed==1]))
}
Year <- c(1992,1994,1996,1998,2000,2002,2004,2006,2008,2010)
par(cex.axis=.75, cex.lab=.75, lheight=.5, mar=c(2,3,2,.5), mgp=c(1,.75,0),
	font.axis=6)
plot(Year, Mean.Ideology, pch=21, col="black", bg="white", ylim=c(-1.5,1.5),
	lab=c(9,5,4), xlab="", ylab="")
mtext("Mean Ideal Point Estimate", side=2, line=1.8, cex=1, font=6)
abline(h=0, lty=2)
title(main="Pure Closed", font.main=7)

screen(1)
Mean.Ideology <- NULL
for (yr in 0:9) {
Mean.Ideology <- c(Mean.Ideology, 
	mean(legyrr$pred_np[legyrr$yrcnt==yr & legyrr$closed==1]))
}
Year <- c(1992,1994,1996,1998,2000,2002,2004,2006,2008,2010)
plot(Year, Mean.Ideology, pch=19, ylim=c(-1.5,1.5), lab=c(9,5,4),
	xlab="", ylab="")

#SEMI-CLOSED
screen(2)
Mean.Ideology <- NULL
for (yr in 0:9) {
Mean.Ideology <- c(Mean.Ideology, 
	mean(legyrd$pred_np[legyrd$yrcnt==yr & legyrd$semicl==1]))
}
Year <- c(1992,1994,1996,1998,2000,2002,2004,2006,2008,2010)
par(cex.axis=.75, cex.lab=.75, lheight=.5, mar=c(2,3,2,.5), mgp=c(1,.75,0),
	font.axis=6)
plot(Year, Mean.Ideology, pch=21, col="black", bg="white", ylim=c(-1.5,1.5),
	lab=c(9,5,4), xlab="", ylab="")
mtext("Mean Ideal Point Estimate", side=2, line=1.8, cex=1, font=6)
abline(h=0, lty=2)
title(main="Semi-Closed", font.main=7)

screen(2)
Mean.Ideology <- NULL
for (yr in 0:9) {
Mean.Ideology <- c(Mean.Ideology, 
	mean(legyrr$pred_np[legyrr$yrcnt==yr & legyrr$semicl==1]))
}
Year <- c(1992,1994,1996,1998,2000,2002,2004,2006,2008,2010)
plot(Year, Mean.Ideology, pch=19, ylim=c(-1.5,1.5), lab=c(9,5,4),
	xlab="", ylab="")
abline(h=0, lty=2)
title(main="Semi-Closed", font.main=7)

#SEMI-OPEN
screen(3)
Mean.Ideology <- NULL
for (yr in 0:9) {
Mean.Ideology <- c(Mean.Ideology, 
	mean(legyrd$pred_np[legyrd$yrcnt==yr & legyrd$semiop==1]))
}
Year <- c(1992,1994,1996,1998,2000,2002,2004,2006,2008,2010)
par(cex.axis=.75, cex.lab=.75, lheight=.5, mar=c(2,3,2,.5), mgp=c(1,.75,0),
	font.axis=6)
plot(Year, Mean.Ideology, pch=21, col="black", bg="white", ylim=c(-1.5,1.5),
	lab=c(9,5,4), xlab="", ylab="")
mtext("Mean Ideal Point Estimate", side=2, line=1.8, cex=1, font=6)
abline(h=0, lty=2)
title(main="Semi-Open", font.main=7)

screen(3)
Mean.Ideology <- NULL
for (yr in 0:9) {
Mean.Ideology <- c(Mean.Ideology, 
	mean(legyrr$pred_np[legyrr$yrcnt==yr & legyrr$semiop==1]))
}
Year <- c(1992,1994,1996,1998,2000,2002,2004,2006,2008,2010)
par(cex.axis=.75, cex.lab=.75, lheight=.5, mar=c(2,3,2,.5), mgp=c(1,.75,0),
	font.axis=6)
plot(Year, Mean.Ideology, pch=19, ylim=c(-1.5,1.5), lab=c(9,5,4),
	xlab="", ylab="")
abline(h=0, lty=2)
title(main="Semi-Open", font.main=7)

#OPEN
screen(4)
Mean.Ideology <- NULL
for (yr in 0:9) {
Mean.Ideology <- c(Mean.Ideology, 
	mean(legyrd$pred_np[legyrd$yrcnt==yr & legyrd$open==1]))
}
Year <- c(1992,1994,1996,1998,2000,2002,2004,2006,2008,2010)
par(cex.axis=.75, cex.lab=.75, lheight=.5, mar=c(2,3,2,.5), mgp=c(1,.75,0),
	font.axis=6)
plot(Year, Mean.Ideology, pch=21, col="black", bg="white", ylim=c(-1.5,1.5),
	lab=c(9,5,4), xlab="", ylab="")
mtext("Mean Ideal Point Estimate", side=2, line=1.8, cex=1, font=6)
abline(h=0, lty=2)
title(main="Pure Open", font.main=7)

screen(4)
Mean.Ideology <- NULL
for (yr in 0:9) {
Mean.Ideology <- c(Mean.Ideology, 
	mean(legyrr$pred_np[legyrr$yrcnt==yr & legyrr$open==1]))
}
Year <- c(1992,1994,1996,1998,2000,2002,2004,2006,2008,2010)
plot(Year, Mean.Ideology, pch=19, ylim=c(-1.5,1.5), lab=c(9,5,4),
	xlab="", ylab="")
abline(h=0, lty=2)
title(main="Pure Open", font.main=7)

#NONPARTISAN
screen(5)
Mean.Ideology <- NULL
for (yr in 0:9) {
Mean.Ideology <- c(Mean.Ideology, 
	mean(legyrd$pred_np[legyrd$yrcnt==yr & legyrd$nonpart==1]))
}
Year <- c(1992,1994,1996,1998,2000,2002,2004,2006,2008,2010)
par(cex.axis=.75, cex.lab=.75, lheight=.5, mar=c(2,3,2,.5), mgp=c(1,.75,0),
	font.axis=6)
plot(Year, Mean.Ideology, pch=21, col="black", bg="white", ylim=c(-1.5,1.5),
	lab=c(9,5,4), xlab="", ylab="")
mtext("Mean Ideal Point Estimate", side=2, line=1.8, cex=1, font=6)
abline(h=0, lty=2)
title(main="Nonpartisan", font.main=7)

screen(5)
Mean.Ideology <- NULL
for (yr in 0:9) {
Mean.Ideology <- c(Mean.Ideology, 
	mean(legyrr$pred_np[legyrr$yrcnt==yr & legyrr$nonpart==1]))
}
Year <- c(1992,1994,1996,1998,2000,2002,2004,2006,2008,2010)
plot(Year, Mean.Ideology, pch=19, ylim=c(-1.5,1.5), lab=c(9,5,4),
	xlab="", ylab="")
abline(h=0, lty=2)
title(main="Nonpartisan", font.main=7)


